<?php
	header('Access-Control-Allow-Origin:*');
    $type = $_GET['type'];
    $uid = $_GET['uid'];
    $con = new mysqli("127.0.0.1", "root", "", "accounting");
    $con->query("SET NAMES 'utf8'");

    //查询所有账单
    if($type == "select"){
        $selectSQL = "SELECT * FROM `diary` WHERE `uid` = '$uid' ORDER BY `did` DESC";
        $result = $con->query($selectSQL);
        $results = array();
        while ($line = mysqli_fetch_assoc($result)) {
            array_push($results,$line);
        }
        echo json_encode($results);
    }
    //查询最近7日
    if($type == "selectLastAccount"){
        $selectSQL = "SELECT * FROM `account_book` WHERE `uid` = '$uid' limit 7";
        $result = $con->query($selectSQL);
        $results = array();
        while ($line = mysqli_fetch_assoc($result)) {
            array_push($results,$line);
        }
        echo json_encode($results);
    }
    //添加
    if($type == "add"){
        $acticle = $_GET["acticle"];
        $feeling = $_GET["feeling"];
        $time = $_GET["time"];

        $addSQL = "INSERT INTO `accounting`.`diary` (`did`, `acticle`, `time`, `feeling`, `uid`) VALUES (NULL, '$acticle', '$time', '$feeling', '$uid')";
        $result = $con->query($addSQL);
    
        echo json_encode($result);
    }
    //删除
    if($type == "delete"){
        $did = $_GET['did'];
        $delSQL = "DELETE FROM `accounting`.`diary` WHERE `diary`.`did` = '$did'";
        $result = $con->query($delSQL);

        echo json_encode($result);
    }
    //更新
    if($type == "update"){
        $did = $_GET['did'];
        $acticle = $_GET["acticle"];
        $feeling = $_GET["feeling"];

        $updateSQL = "UPDATE `accounting`.`diary` SET `acticle` = '$acticle', `feeling` = '$feeling' WHERE `diary`.`did` = $did";
        $result = $con->query($updateSQL);

        echo json_encode($result);
    }
?>